$(function () {
    $("#jqGrid").Grid({
        url: '../remit/conversionlist',
        colModel: [{
            label: 'id', name: 'id', index: 'id', key: true, hidden: true
        }, {
            label: '转账人', name: 'sysName', index: 'sysName',align:'center', width: 40
        }, {
            label: '转账人手机号', name: 'sysMobile', index: 'sysMobile',align:'center', width: 60
        }, {
            label: '收款人', name: 'nideshopName', index: 'nideshopName',align:'center', width: 40
        }, {
            label: '收款人手机号', name: 'nideshopMobile', index: 'nideshopMobile',align:'center', width: 60
        }, {
            label: '申请时间', name: 'applyTime', index: 'applyTime',align:'center', width: 70, formatter: function (value) {
                return transDate(value);
            }
        }, {
            label: '完成时间', name: 'remitTime', index: 'remitTime',align:'center', width: 70, formatter: function (value) {
                return transDate(value);
            }
        }, {
            label: '购物劵类型', name: 'remitType', index: 'remitType', align:'center',width: 50, formatter: function (value) {
                return trRemitType(value);
            }
        }, {
            label: '申请进度', name: 'remitStatus', index: 'remitStatus', width: 40,align:'center',formatter: function (value) {
                return transRemitStatus(value);
            }
        },{
            label: '购物劵金额', name: 'remitFee', index: 'remitFee', width: 40,align:'center'
        },{
            label: '银行类型', name: 'bankName', index: 'bankName',align:'center', width: 40
        },{
            label: '开户行', name: 'openingBank', index: 'openingBank',align:'center', width: 100
        },{
            label: '账户', name: 'account', index: 'account',align:'center', width: 90
        },{
            label: '户主', name: 'userName', index: 'userName',align:'center', width: 40
        },{
            label: '银行卡类型', name: 'cardTypeName', index: 'cardTypeName',align:'center', width: 40
        }]
    });
    var newHeight = $(window).height()-130;
    $(".ui-jqgrid .ui-jqgrid-bdiv").css("cssText","height: "+newHeight+"px!important;");
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        q: {
            sysMobile: '',
            nideshopMobile:'',
            dateRange:'',
            beginTime:'',
            endTime:'',
            remitStatus:''
        },
        status:[
            {id:0,name:'受理中'},
            {id:1,name:'汇款中'},
            {id:2,name:'已汇款'},
            {id:3,name:'失败'},
        ],
        remit:{},
        ruleValidate: {
        },
    },
    methods: {
        query: function () {
            if(vm.q.dateRange[0]){
                vm.q.beginTime = vm.dateFtt("yyyy-MM-dd hh:mm:ss",vm.q.dateRange[0]);
            }else{
                vm.q.beginTime = '';
            };
            if(vm.q.dateRange[1]){
                vm.q.endTime = vm.dateFtt("yyyy-MM-dd hh:mm:ss",vm.q.dateRange[1]);
            }else{
                vm.q.endTime = '';
            };
            console.log("q",vm.q);
            vm.reload();
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    'sysMobile': vm.q.sysMobile,
                    'nideshopMobile': vm.q.nideshopMobile,
                    'beginTime':vm.q.beginTime,
                    'endTime':vm.q.endTime,
                    'remitStatus':vm.q.remitStatus,
                },
                page: page,
            }).trigger("reloadGrid");
        },
        dateFtt:function(fmt,date){
            var o = {
                "M+" : date.getMonth()+1,                 //月份
                "d+" : date.getDate(),                    //日
                "h+" : date.getHours(),                   //小时
                "m+" : date.getMinutes(),                 //分
                "s+" : date.getSeconds(),                 //秒
                "q+" : Math.floor((date.getMonth()+3)/3), //季度
                "S"  : date.getMilliseconds()             //毫秒
            };
            if(/(y+)/.test(fmt))
                fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));
            for(var k in o)
                if(new RegExp("("+ k +")").test(fmt))
                    fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
            return fmt;
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.getInfo(id);
        },
        getInfo: function (id) {
            var param = {'id': id,'limit':1, 'page':1};
            Ajax.request({
                url: "../remit/conversionlist",
                type: "POST",
                async: true,
                params:param,
                successCallback: function (r) {
                    if(r.errno==0 && r.page.currPage==1){
                        vm.remit = r.page.list[0];
                        vm.remit.applyTime= transDate(vm.remit.applyTime);
                        vm.remit.remitTime= transDate(vm.remit.remitTime);
                        vm.remit.remitType= trRemitType(vm.remit.remitType);
                    }else{
                        layer.alert("请求数据异常,请联系管理员", {icon: 5});
                    }
                }
            });
        },
        saveOrUpdate: function (event) {
            var url = vm.user.id == null ? "../remit/save" : "../remit/update";

            Ajax.request({
                type: "POST",
                url: url,
                contentType: "application/json",
                params: JSON.stringify(vm.remit),
                successCallback: function (r) {
                    alert(r.errmsg, function (index) {
                        if(r.errno==0){
                            vm.reload();
                        }
                    });
                }
            });
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        remitted(){
            var that = this;
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            var rowData = $("#jqGrid").jqGrid('getRowData',id);
            if(rowData.remitStatus.match(/已汇款|失败/)){
                this.$Message.warning({
                    content:"当前状态不可编辑"
                })
                return;
            };
            this.$Modal.confirm({
                title:'提示',
                content:'请确认资金已经到账，将进行完成操作',
                loading:true,
                onOk:function () {
                    Ajax.request({
                        type: "POST",
                        url: "../remit/remitted",
                        contentType: "application/json",
                        params: JSON.stringify({id:id}),
                        successCallback: function (r) {
                            that.$Modal.remove();
                            alert(r.errmsg, function (index) {
                                if(r.errno==0){
                                    vm.reload();
                                }
                            });
                        }
                    });
                },
            });
        },
        defeated(){
            var that = this;
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            var rowData = $("#jqGrid").jqGrid('getRowData',id);
            if(rowData.remitStatus.match(/已汇款|失败/)){
                this.$Message.warning({
                    content:"当前状态不可编辑"
                })
                return;
            };
            this.$Modal.confirm({
                title:'提示',
                content:'改变状态为失败，购物劵将退回原账户',
                loading:true,
                onOk:function () {
                    Ajax.request({
                        type: "POST",
                        url: "../remit/defeated",
                        contentType: "application/json",
                        params: JSON.stringify({id:id}),
                        successCallback: function (r) {
                            that.$Modal.remove();
                            alert(r.errmsg, function (index) {
                                if(r.errno==0){
                                    vm.reload();
                                }
                            });
                        }
                    });
                },
            });
        },

    }
});
